import TabController from '@/editor/js/tab/service-tab-list'
import WorkspaceExplorer from '@/editor/common/js/workspace/explorer'
import workspaceManager from '@/editor/common/js/workspace/workspace'
import BrowserStorage from '@/editor/common/js/workspace/browser-storage'
import configuation from './config'
import _ from 'lodash'
import store from '@/store/index'
export default class Application {

  constructor() {
    this.config = configuation
    store.dispatch('siddhi/setConfiguration', this.config)
    var self = this;
    var pathSeparator;
    this.initComponents();
    this.defaultResponse = {
      "siddhiAppConfig": {
        "siddhiAppName": "",
        "siddhiAppDescription": "",
        "appAnnotationList": [],
        "streamList": [],
        "tableList": [],
        "windowList": [],
        "triggerList": [],
        "aggregationList": [],
        "functionList": [],
        "partitionList": [],
        "sourceList": [],
        "sinkList": [],
        "queryLists": {
          "WINDOW_FILTER_PROJECTION": [],
          "PATTERN": [],
          "SEQUENCE": [],
          "JOIN": []
        },
        "finalElementCount": 0
      },
      "edgeList": []
    }
  }

  initComponents = function () {
    //init workspace manager
    this.workspaceManager = new workspaceManager(this);

    this.browserStorage = new BrowserStorage('spToolingTempStorage');

    //init tab controller
    var tabControlOpts = _.get(this.config, "tab_controller");
    _.set(tabControlOpts, 'application', this);
    //初始化对象

    this.tabController = new TabController(tabControlOpts);

    //init workspace explorer
    var workspaceExplorerOpts = _.get(this.config, "workspace_explorer");
    _.set(workspaceExplorerOpts, 'application', this);

    this.workspaceExplorer = new WorkspaceExplorer(workspaceExplorerOpts);
    store.dispatch('siddhi/setApplication', this)
  };

  render = function () {
    this.tabController.render();
    this.app = store.getters.app
  };

  getOperatingSystem = function () {
    var operatingSystem = "Unknown OS";
    if (navigator.appVersion.indexOf("Win") != -1) {
      operatingSystem = "Windows";
    }
    else if (navigator.appVersion.indexOf("Mac") != -1) {
      operatingSystem = "MacOS";
    }
    else if (navigator.appVersion.indexOf("X11") != -1) {
      operatingSystem = "UNIX";
    }
    else if (navigator.appVersion.indexOf("Linux") != -1) {
      operatingSystem = "Linux";
    }
    return operatingSystem;
  };

  displayInitialView = function () {
    this.workspaceManager.displayInitialTab();
  };
  displayInitialViewOverride = function (name) {
    this.workspaceManager.displayInitialWorkspace(this.app, name);
  };

  isRunningOnMacOS = function () {
    return _.isEqual(this.getOperatingSystem(), 'MacOS');
  };

  getPathSeperator = function () {
    if (this.pathSeparator != undefined) {
      return this.pathSeparator;
    } else {
      this.pathSeparator = this.workspaceExplorer._serviceClient.readPathSeparator();
      return this.pathSeparator;
    }
  };

  applicationConstants = function () {
    var constants = {
      messageLinkType: {
        OutOnly: 1,
        InOut: 2
      }
    };

    return constants;
  }
}
